package appjam;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListJamServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Jam.class);
		List<Jam> jams = new ArrayList<Jam>();
		jams = (List) query.execute();

		String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		xml += "<jams>";
		
		for(Jam j : jams) {
			xml += "<jam>";
			xml += "<title>" + j.getTitle() + "</title>";
			xml += "<description>" + j.getDescription() + "</description>";
			xml += "<latitude>" + j.getLatitude() + "</latitude>";
			xml += "<longitude>" + j.getLongitude() + "</longitude>";
			xml += "</jam>";
		}
		
		xml += "</jams>";
		
		PrintWriter out = resp.getWriter();
		out.println(xml);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}

}
